package fr.nekloth.android.hinode;


import uk.me.jstott.coordconv.LatitudeLongitude;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


/*
DEV : android:apiKey="0efJv19AuYJchL0ZmhlDYHsjSouWY0t3XeIcwYg"
PUB : android:apiKey="0efJv19AuYJcH0oxR4oYguhq_NafZh1Gup9xcBw"
*/

public class coordinatesGMaps extends MapActivity  {

	public LatitudeLongitude ll_to_use = new LatitudeLongitude(0.0d, 0.0d);
    public MapView mapView;

    public int returnCode = 1;
	
	public void doActions(int clickedBtn) {
		if (clickedBtn == R.id.btnCancel) {
			setResult(1);
			finish();
		}

		if (clickedBtn == R.id.btnOk) {
			GeoPoint gp = mapView.getMapCenter();
			ll_to_use.setLatitude(gp.getLatitudeE6()/1E6);
			ll_to_use.setLongitude(gp.getLongitudeE6()/1E6);
			//Log.d("Nekloth","Coord : " + ll_to_use.toSimplifyString());
			coordinatesActivity.ll_from_GMAPS = ll_to_use;
			setResult(0);
			finish();
		}		
	}
		
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coordinates_via_gmaps);
        
        mapView = (MapView) findViewById(R.id.mapview);
        
        //Defining the FONT to the title TextView
        Typeface face=Typeface.createFromAsset(getAssets(), applicationStartup.GLOBAL_FONT_TO_USE);
        TextView tv=(TextView)findViewById(R.id.screentitle);
        tv.setTypeface(face);

        
        mapView.setBuiltInZoomControls(true);
        MapController mp = mapView.getController();
        
        //Set the zoom level
        mp.setZoom(applicationStartup.MAPS_ZOOM_LEVEL-2);

        //Center the map
        Double lng = applicationStartup.currentPosition.getLongitude()*1E6;
        Double lat = applicationStartup.currentPosition.getLatitude()*1E6;
        GeoPoint gpCenter = new GeoPoint(lat.intValue(), lng.intValue());
        mp.setCenter(gpCenter);
        
        Button okBtn = (Button) findViewById(R.id.btnOk);
        Button koBtn = (Button) findViewById(R.id.btnCancel);
        
	    koBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {doActions(R.id.btnCancel);}
		});

	    okBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {doActions(R.id.btnOk);}
		});
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event){ 
		if(keyCode==KeyEvent.KEYCODE_BACK) {
			setResult(1);
			finish();
			return true; 
		} 
		return false;
	}
	
}



